﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Outputs.QualityContext;
using SlamDunk.MES.IAppService.Interfaces;

namespace SlamDunk.MES.Client.ViewModels.WorkOrderInspection
{
    /// <summary>
    /// 工序质检审核ViewModel
    /// </summary>
    public class CheckViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 质量管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IQualityContract> _iQualityContract;

        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public CheckViewModel(ServiceProxy<IQualityContract> iQualityContract, ServiceProxy<IExecutionContract> iExecutionContract, IWindowManager windowManager)
        {
            this._iQualityContract = iQualityContract;
            this._iExecutionContract = iExecutionContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region 属性
        #region 质量检查编号
        /// <summary>
        /// 质量检查编号
        /// </summary>
        [DependencyProperty]
        public string InspectionNo { get; set; }
        #endregion

        #region 质量检查名称
        /// <summary>
        /// 质量检查名称
        /// </summary>
        [DependencyProperty]
        public string InspectionName { get; set; }
        #endregion

        #region 质检日期
        /// <summary>
        /// 质检日期
        /// </summary>
        [DependencyProperty]
        public DateTime? InspectedDate { get; set; }
        #endregion

        #region 工序信息Id
        /// <summary>
        /// 工序信息Id
        /// </summary>
        [DependencyProperty]
        public Guid? WorkOrderId { get; set; }
        #endregion

        #region 工序信息
        /// <summary>
        /// 工序信息
        /// </summary>
        [DependencyProperty]
        public string WorkOrderName { get; set; }
        #endregion

        #region 描述
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 质量检查明细列表
        /// <summary>
        /// 质量检查明细列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<QualityInspectionDetailInfo> DetailParams { get; set; }
        #endregion

        #region 工序质检
        /// <summary>
        /// 工序质检
        /// </summary>
        public WorkOrderInspectionInfo WorkOrderInspection { get; set; }
        #endregion

        #region 审核意见
        /// <summary>
        /// 审核意见
        /// </summary>
        [DependencyProperty]
        public string CheckedRemark { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(WorkOrderInspectionInfo workOrderInspectionInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(WorkOrderInspectionInfo workOrderInspectionInfo)
        {
            this.WorkOrderInspection = workOrderInspectionInfo;
            this.InspectionNo = this.WorkOrderInspection.Number;
            this.InspectionName = this.WorkOrderInspection.Name;
            this.InspectedDate = this.WorkOrderInspection.InspectedDate;
            this.WorkOrderName = this.WorkOrderInspection.WorkOrderInfo.Name;
            this.Description = this.WorkOrderInspection.Description;

            IEnumerable<QualityInspectionDetailInfo> detailInfos = await Task.Run(() => this._iQualityContract.Channel.GetQualityInspectionDetails(workOrderInspectionInfo.Id));
            this.DetailParams = new ObservableCollection<QualityInspectionDetailInfo>(detailInfos);
        }
        #endregion

        #region 通过 —— async void Approve()
        /// <summary>
        /// 通过
        /// </summary>
        public async void Approve()
        {
            this.Busy();

            await Task.Run(() => this._iQualityContract.Channel.CheckQualityInspection(this.WorkOrderInspection.Id, true, this.CheckedRemark));

            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #region 拒绝 —— async void Reject()
        /// <summary>
        /// 拒绝
        /// </summary>
        public async void Reject()
        {
            this.Busy();

            await Task.Run(() => this._iQualityContract.Channel.CheckQualityInspection(this.WorkOrderInspection.Id, false, this.CheckedRemark));

            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #endregion
    }
}
